package com.example.cpms.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.cpms.entity.Sum;
import com.example.cpms.entity.User;
import com.example.cpms.mapper.UserMapper;
import com.example.cpms.service.UserService;
import com.example.cpms.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 登陆功能
     * @param user
     * @return
     */
    @Override
    public SaResult usernameLoginService(User user) {
        String username=user.getUsername(); //获取用户名
        String password=user.getPassword();  //获取密码
        if (username.equals(userMapper.getUsername(username)) &&
                password.equals(userMapper.getPassword(username))) { //在数据库比对用户名和密码信息
            StpUtil.login(userMapper.getId(username));   //根据用户名拿到用户的id，使用id为标识生成Token
            System.err.println(StpUtil.getTokenInfo());  //控制台输出token信息
            return  SaResult.data(StpUtil.getTokenValue()); //将token响应给前端

        }
        return SaResult.error("账号或密码错误，请重试");
    }

    @Override
    public Result getAll() {
        Sum sum = new Sum();
        sum.setPropety(userMapper.getPropety());
        sum.setBuilding(userMapper.getBuilding());
        sum.setRod(userMapper.getRod());
        sum.setWarning(userMapper.getWarning());
        return Result.success(sum);
    }

}
